<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-model-item label="教职工" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="userId">
              <j-dict-select-tag type="list" v-model="model.userId" dictCode="sys_user,realname,id" placeholder="请选择教工" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="请假类型" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="leaveType">
              <j-dict-select-tag type="list" v-model="model.leaveType" dictCode="leave_type" placeholder="请选择请假类型" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="请假开始时间" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="startTime">
              <j-date :showTime="true" date-format="YYYY-MM-DD HH:mm:ss" placeholder="请选择请假开始时间" v-model="model.startTime" 
                @change="caculateDay" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="请假结束时间" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="endTime">
              <j-date :showTime="true" date-format="YYYY-MM-DD HH:mm:ss" placeholder="请选择请假结束时间" v-model="model.endTime" 
                @change="caculateDay" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <!-- <a-col :span="24">
            <a-form-model-item label="请假时长" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="length">
              <a-input v-model="model.length" placeholder="请输入请假时长" suffix="天" type="number" style="width: 100%" />
            </a-form-model-item>
          </a-col> -->
          <a-col :span="24">
            <a-form-model-item label="请假事由" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="leaveExplain">
              <a-textarea v-model="model.leaveExplain" rows="4" placeholder="请输入请假事由" />
            </a-form-model-item>
          </a-col>
        </a-row>
        <a-row v-if="model.id">
          <a-col :span="24">
            <a-form-model-item label="审批状态" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="approvalState">
              <j-dict-select-tag type="list" v-model="model.approvalState" dictCode="approval_state" placeholder="请输入审批状态" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="审批结果" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="approvalResult">
              <j-dict-select-tag type="list" v-model="model.approvalResult" dictCode="approval_result" placeholder="请输入审批状态" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="审批人" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="approvar">
              <a-input v-model="model.approvar_dictText" placeholder="请输入审批人"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="审批信息" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="approvalInfo">
              <a-textarea v-model="model.approvalInfo" rows="4" placeholder="请输入审批信息" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="审批时间" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="approvalTime">
              <a-input v-model="model.approvalTime"></a-input>
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

  import { httpAction, getAction } from '@/api/manage'

  export default {
    name: 'SkStaffLeaveForm',
    components: {
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        model:{
         },
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
        },
        url: {
          add: "/management/skStaffLeave/add",
          edit: "/management/skStaffLeave/edit",
          queryById: "/management/skStaffLeave/queryById"
        }
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
    },
    created () {
       //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
    },
    methods: {
      add () {
        this.edit(this.modelDefault);
      },
      edit (record) {
        this.model = Object.assign({}, record);
        this.visible = true;
      },
      caculateDay () {
        let a1 = Date.parse(this.model.endTime);
        let a2 = Date.parse(this.model.startTime);
        console.log(a1, a2)
        if((a1-a2)>0){
          let day = (a1-a2)/ (1000 * 60 * 60 * 24);//核心：时间戳相减，然后除以天数
          // let leave1 = (a1-a2) % (24 * 3600 * 1000); //计算天数后剩余的毫秒数
          // let hours = Math.floor(leave1 / (3600 * 1000)); //计算出小时数
          // let leave2 = leave1 % (3600 * 1000); //计算小时数后剩余的毫秒数
          // let minutes = Math.floor(leave2 / (60 * 1000)); //计算相差分钟数
          // let leave3 = leave2 % (60 * 1000); //计算分钟数后剩余的毫秒数
          // let seconds = Math.round(leave3 / 1000);
          this.model.length = day.toFixed(2);
        }else {
          this.model.length = 0;
        }
      },
      submitForm () {
        const that = this;
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
              method = 'put';
            }
            httpAction(httpurl,this.model,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
            })
          }
         
        })
      },
    }
  }
</script>